import websocket
from websocket import WebSocketApp, ABNF

try:
    import thread
except ImportError:
    import _thread as thread
import time


class Test(object):
    def __init__(self):
        super(Test, self).__init__()
        self.url = "wss://192.10.2.125/sig/v1/rtc?room=live123&display=18d4778"
        self.ws = None

    def on_message(self, message):
        print("####### on_message #######")
        print("message：%s" % message)

    def on_error(self, error, obj):
        print("####### on_error #######")
        print("error：%s" % obj)

    def on_close(self):
        print("####### on_close #######")

    def on_ping(self, message):
        print("####### on_ping #######")
        print("ping time：%s" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        print("ping message：%s" % message)

    def on_pong(self, message):
        print("####### on_pong #######")
        print("pong time：%s" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        print("pong message：%s" % message)

    def on_open(self):
        print("####### on_open #######")

        thread.start_new_thread(self.run, ())

    def run(self, *args):
        # for i in range(3):
        #     time.sleep(1)
        #     self.ws.send("Hello %d" % i)

        while True:
            time.sleep(1)
            input_msg = input("输入要发送的ping消息（ps：输入关键词 close 结束程序）:\n")
            if input_msg == "close":
                self.ws.close()  # 关闭
                print("thread terminating...")
                break
            else:
                self.ws.send(input_msg, ABNF.OPCODE_PING)
                # self.ws.send(input_msg)

    def start(self):
        websocket.enableTrace(True)  # 开启运行状态追踪。debug 的时候最好打开他，便于追踪定位问题。

        self.ws = WebSocketApp(self.url,
                               on_open=self.on_open,
                               on_message=self.on_message,
                               on_error=self.on_error,
                               on_close=self.on_close,
                               on_ping=self.on_ping,
                               on_pong=self.on_pong)

        self.ws.run_forever(ping_interval=20, ping_timeout=10)


if __name__ == '__main__':
    Test().start()


